/**
 *
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} mobile
 * @returns {Boolean}
 */
export function validMobile(mobile) {
  const reg = /^1[3456789]\d{9}$/
  return reg.test(mobile)
}

/**
 * @param {string} number
 * @returns {Boolean}
 */
export function validNumber(number) {
  const reg = /^[0-9]+$/
  return reg.test(number)
}

/**
 * @param {string} number
 * @returns {Boolean}
 */
export function validPassword(password) {
  const reg = /^[0-9A-Za-z]{6,}$/
  return reg.test(password)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * @param {JSON} str
 * @returns {Boolean}
 */
export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      var obj = JSON.parse(str)
      if (typeof obj === 'object' && obj) {
        return true
      } else {
        return false
      }
    } catch (e) {
      return false
    }
  }
  return
}

/**
 * @param {Array} str
 * @returns {Boolean}
 */
export function isEmpty(str) {
  if (str === '' || str === null || str === undefined || str.length === 0) {
    return true
  }
  return false
}

/**
 * @param {*} validName
 * @returns {*} data type
 */
export function validDataType(validName) {
  // Object.prototype.toString.call('') ;   // [object String]
  // Object.prototype.toString.call(1) ;    // [object Number]
  // Object.prototype.toString.call(true) ; // [object Boolean]
  // Object.prototype.toString.call(Symbol()); //[object Symbol]
  // Object.prototype.toString.call(undefined) ; // [object Undefined]
  // Object.prototype.toString.call(null) ; // [object Null]
  // Object.prototype.toString.call(new Function()) ; // [object Function]
  // Object.prototype.toString.call(new Date()) ; // [object Date]
  // Object.prototype.toString.call([]) ; // [object Array]
  // Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
  // Object.prototype.toString.call(new Error()) ; // [object Error]
  // Object.prototype.toString.call(document) ; // [object HTMLDocument]
  // Object.prototype.toString.call(window) ; //[object global] window 是全局对象 global 的引用
  return Object.prototype.toString.call(validName)
}
